package com.aiss.ws.wxapi;


import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Toast;

import com.aiss.ws.R;
import com.aiss.ws.activity.AgreenOnActivity_;
import com.aiss.ws.activity.MycarActivity_;
import com.aiss.ws.activity.SurePayActivity_;
import com.aiss.ws.wx.Constants;
import com.tencent.mm.sdk.constants.ConstantsAPI;
import com.tencent.mm.sdk.modelbase.BaseReq;
import com.tencent.mm.sdk.modelbase.BaseResp;
import com.tencent.mm.sdk.openapi.IWXAPI;
import com.tencent.mm.sdk.openapi.IWXAPIEventHandler;
import com.tencent.mm.sdk.openapi.WXAPIFactory;


public class WXPayEntryActivity extends Activity implements IWXAPIEventHandler {
	
	private static final String TAG = "MicroMsg.SDKSample.WXPayEntryActivity";
	
    private IWXAPI api;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.pay_result);
        
    	api = WXAPIFactory.createWXAPI(this, Constants.APP_ID);
        api.handleIntent(getIntent(), this);
    }

	@Override
	protected void onNewIntent(Intent intent) {
		super.onNewIntent(intent);
		setIntent(intent);
        api.handleIntent(intent, this);
	}

	@Override
	public void onReq(BaseReq req) {
	}

	@Override
	public void onResp(BaseResp resp) {


		if (resp.getType() == ConstantsAPI.COMMAND_PAY_BY_WX) {
//			AlertDialog.Builder builder = new AlertDialog.Builder(this);
//			builder.setTitle("提示");
//			builder.setMessage(getString(R.string.pay_result_callback_msg, String.valueOf(resp.errCode)));
//			builder.show();

			final int code = resp.errCode;
			String msg = "";
			switch (code) {
				case 0:
					//请求三次
					msg = "支付成功！";
					if(SurePayActivity_.getActivity()!=null&&!SurePayActivity_.getActivity().isFinishing()){
						SurePayActivity_.getActivity().finish();
					}
					if(AgreenOnActivity_.getActivity()!=null&&!AgreenOnActivity_.getActivity().isFinishing()){
						AgreenOnActivity_.getActivity().finish();
					}

					if(MycarActivity_.getActivity()!=null&&!MycarActivity_.getActivity().isFinishing()){
						MycarActivity_.getActivity().finish();
					}

					break;
				case -1:
					msg = "支付失败！";
					break;
				case -2:
					msg = "您取消了支付！";
					break;
				default:
					msg = "支付失败！";
					break;

			}
			finish();
			Toast.makeText(WXPayEntryActivity.this, msg, Toast.LENGTH_SHORT).show();
		}
	}
}